//
//  PKPlarRadioViewController.m
//  PianKe
//
//  Created by mingtao on 15/10/7.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKPlarRadioViewController.h"

#import "PKRadioMainViewCell.h"

#import "PKRequestManager.h"

#import "PKRadiomanScou.h"

#import "PKRadioModel.h"

#import "PKDetailTableViewController.h"

@interface PKPlarRadioViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *mainTableView;//主页面

@property(nonatomic,strong)NSMutableArray *dataSuorceArray;//提供数据的

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSMutableArray *scArrats;

@property(nonatomic,strong)UIView *topView;

@property(nonatomic,strong)UIButton *but;

@end

@implementation PKPlarRadioViewController

-(NSMutableArray *)scArrats
{
    if (_scArrats == nil) {
        _scArrats = [NSMutableArray array];
    }
    return _scArrats;
    
}

-(NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
    
}

-(NSMutableArray *)dataSuorceArray
{
    if (_dataSuorceArray == nil) {
        _dataSuorceArray = [NSMutableArray array];
    }
    return _dataSuorceArray;
}

//cell自定义视图
-(void) requestDta
{
    NSMutableDictionary *parDic =[NSMutableDictionary dictionary];
    parDic[@"auth"]=@"";
    parDic[@"client"]=@"1";
    parDic[@"deviceid"]=@"63A94D37-33F9-40FF-9EBB-481182338873";
    
    parDic[@"version"]=@"3.0.2";
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio" parDic:parDic finish:^(NSData *data) {
        NSDictionary *DIC=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *listArray=DIC[@"data"][@"alllist"];
        // NSArray *aa=[@"alllist"][@"userinfo"];
        
        for (NSDictionary *modelDic in listArray) {
            PKRadioMainModel *radioMain=[[PKRadioMainModel alloc]init];
            
            [radioMain setValuesForKeysWithDictionary:modelDic];
            
            [self.dataSuorceArray addObject:radioMain];
            
        }
        
        PKRadiomanScou *pkRadio=[[PKRadiomanScou alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI/2-10)];
        NSArray *tsarray=DIC[@"data"][@"hotlist"];
        for (NSDictionary *dictkeys in tsarray) {
            
            PKRadioModel *redimodel=[[PKRadioModel alloc]init];
            
            [redimodel setValuesForKeysWithDictionary:dictkeys];
            
            [self.dataArray addObject:redimodel];
        }
        pkRadio.array = self.dataArray;
        
        NSArray *scuArray=DIC[@"data"][@"carousel"];
        
        for (NSDictionary *ScDic in scuArray) {
            
            PKRadioMainModelScull *pkRadioScull=[[PKRadioMainModelScull alloc]init];
            
            [pkRadioScull setValuesForKeysWithDictionary:ScDic];
            
            [self.scArrats addObject:pkRadioScull];
            
        }
        pkRadio.ScrArray = self.scArrats;
        
        self.mainTableView.tableHeaderView= pkRadio;
        
        
        
        [_mainTableView reloadData];
        
        
        
        
    } error:^(NSError *error) {
        NSLog(@"电台主页面请求数据失败%@",error);
    }];
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   // self.navigationController.navigationBar.hash=NO;
    self.navigationItem.title = @"电台";
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestDta];
    
    _topView =[[UIView alloc]initWithFrame:CGRectMake(0, 20, WIHES, 44)];
    
    _topView.backgroundColor = [UIColor blackColor];
    
    _but=[UIButton buttonWithType:(UIButtonTypeSystem)];
    
    _but.frame = CGRectMake(30, 10, 30, 30);
    
    [_but setBackgroundImage:[[UIImage imageNamed:@"ImageBut"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)] forState:UIControlStateNormal];
    
    [_but addTarget:self action:@selector(butImage) forControlEvents:UIControlEventTouchUpInside];
    
    [_topView addSubview:_but];
    
        [self.view addSubview:_topView];
    
    
    
    //  [self.view addSubview:pkRadio];
    
    
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI-70)];
    
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    
    _mainTableView.rowHeight = 110;
    
    [self.view addSubview:_mainTableView];
    
    //    _dataSuorceArray = [NSMutableArray array];
    
    
    [_mainTableView registerClass:[PKRadioMainViewCell class] forCellReuseIdentifier:@"pkReadioMainCell"];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return  self.dataSuorceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioMainViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"pkReadioMainCell" forIndexPath:indexPath];
    
    cell.Radiomain = self.dataSuorceArray[indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PKDetailTableViewController *Detail = [[PKDetailTableViewController alloc]init];
    
    PKRadioMainModel *RadioMolde=self.dataSuorceArray[indexPath.row];
    
    Detail.RadioDitail = RadioMolde;
    //radioid
    Detail.Did = RadioMolde.radioid;
    
    [self.navigationController pushViewController:Detail animated:YES];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)butImage
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
